package org.jeecg.modules.doctor;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.doctor.entity.DoctorAccount;
import org.jeecg.common.modules.doctor.service.IDoctorAccountService;
import org.jeecg.common.modules.doctor.vo.DoctorAccountInfoReqVo;
import org.jeecg.common.modules.withdraw.service.IWithdrawRecordService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.data.response.DoctorAccountRes;
import org.jeecg.modules.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: doctor_account
 * @Author: jeecg-boot
 * @Date:   2020-08-25
 * @Version: V1.0
 */
@Api(tags="doctor_account")
@RestController
@RequestMapping("/daccount/doctorAccount")
@Slf4j
public class DoctorAccountController extends JeecgController<DoctorAccount, IDoctorAccountService> {
	//账户服务类
	@Autowired
	private IDoctorAccountService doctorAccountService;

	@Autowired
	private IWithdrawRecordService withdrawRecordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param doctorAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "doctor_account-分页列表查询")
	@ApiOperation(value="doctor_account-分页列表查询", notes="doctor_account-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
			DoctorAccount doctorAccount,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
		    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
		    HttpServletRequest req) {
		//分页数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//（1）先查询所有的用户信息
		String name = req.getParameter("name");  //查询的用户名称

		/*
		DoctorAccountInfoReqVo   ：自定义的查询返回类型
		getDoctorAccountInfos()  ：账户服务类中自定义的查询方法
		name,pageNo,pageSize     ：传递查询的参数
		 */
		List<DoctorAccountInfoReqVo> doctorAccountInfoReqVos = doctorAccountService.getDoctorAccountInfos(name,dataStart,pageSize);

		List response = new ArrayList();
		//判断每条账户的提现状态
		for(DoctorAccountInfoReqVo reqVo : doctorAccountInfoReqVos ){
			Integer stateCount = withdrawRecordService.getAccountDrawStateCount(reqVo.getId());
			DoctorAccountRes info = this.initDoctorAccountInfo(reqVo,stateCount);
			response.add(info);
		}

		//（2）获取总条数
		Integer count = doctorAccountService.getDoctorAccountCount(name);

		//（3）组织分页的返回数据
		ResultVo vo = this.initResultVo(response,count,pageNo,pageSize);
		return Result.ok(vo);
	}

	/**
	 * 组合账户的信息
	 * @param reqVo
	 * @param stateCount
	 * @return
	 */
	private DoctorAccountRes initDoctorAccountInfo(DoctorAccountInfoReqVo reqVo, Integer stateCount){
		DoctorAccountRes info = new DoctorAccountRes();
		boolean state;
		if(stateCount == 0){
			state = true;//全部提现
		}else{
			state = false; //提现中
		}
		info.setId(reqVo.getId());
		info.setDoctorId(reqVo.getDoctorId());
		info.setName(reqVo.getName());
		info.setTotalAmount(reqVo.getTotalAmount());
		info.setBalance(reqVo.getBalance());
		info.setState(state);
		return info;
	}

	/**
	 * 组合返回的信息
	 * @param list
	 * @param count
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	private ResultVo initResultVo(List list, Integer count, Integer pageNo, Integer pageSize){
		ResultVo vo = new ResultVo();
		double dCount    = Double.valueOf(count);       //查询的数据总条数
		double dPageSize = Double.valueOf(pageSize);    //每页显示的条数
		int pages = (int) Math.ceil(dCount / dPageSize);//页码数量

		vo.setCurrent(pageNo);
		vo.setOrders(new ArrayList<>());
		vo.setRecords(list);
		vo.setPages(pages);
		vo.setSize(pageSize);
		vo.setTotal(count);
		return vo;
	}
	
	/**
	 *   添加
	 *
	 * @param doctorAccount
	 * @return
	 */
	@AutoLog(value = "doctor_account-添加")
	@ApiOperation(value="doctor_account-添加", notes="doctor_account-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DoctorAccount doctorAccount) {
		doctorAccountService.save(doctorAccount);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param doctorAccount
	 * @return
	 */
	@AutoLog(value = "doctor_account-编辑")
	@ApiOperation(value="doctor_account-编辑", notes="doctor_account-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DoctorAccount doctorAccount) {
		doctorAccountService.updateById(doctorAccount);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_account-通过id删除")
	@ApiOperation(value="doctor_account-通过id删除", notes="doctor_account-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		doctorAccountService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "doctor_account-批量删除")
	@ApiOperation(value="doctor_account-批量删除", notes="doctor_account-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.doctorAccountService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_account-通过id查询")
	@ApiOperation(value="doctor_account-通过id查询", notes="doctor_account-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DoctorAccount doctorAccount = doctorAccountService.getById(id);
		if(doctorAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(doctorAccount);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param doctorAccount
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DoctorAccount doctorAccount) {
        return super.exportXls(request, doctorAccount, DoctorAccount.class, "doctor_account");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DoctorAccount.class);
    }

}
